package com.wbt.img;

import java.awt.image.BufferedImage;
import java.io.File;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @program: Thread-demo
 * @description:
 * @author: Wang·Baotong
 * @create: 2021-05-26 16:58
 */
public class TestImg {

    public static List<int[]> imageCutType = new ArrayList();

    public static void main(String[] args) throws Exception {

        String fileName = "aa.jpg";
        File localFile = new File(fileName);
        String suffix = localFile.getName().substring(localFile.getName().lastIndexOf(".") + 1);

        String targetDir = UUID.randomUUID() + File.separator;
        File targetFileDir=new File(targetDir);
        if(!targetFileDir.exists()){
            targetFileDir.mkdirs();
        }

        List<int[]> imageCutType = getImageCutType();
        for (int[] cutType:imageCutType){
            try {

              /*  ImageInputStream iis = ImageIO.createImageInputStream(fis);
                // 根据图片类型获取该种类型的ImageReader
                ImageReader reader = ImageIO.getImageReadersBySuffix(suffix).next();
                reader.setInput(iis,true);
                ImageReadParam param = reader.getDefaultReadParam();
                //param.setSourceRegion(new Rectangle(x, y, width, height));
                BufferedImage bi = reader.read(0, param);

*/
                String name = cutType[0] + "-" + cutType[1] + "-" + cutType[2];

                String targetFile = targetDir + name + "."+ suffix;

                //new ImageUtil().thumbnailImage(new File(fileName) , new java.io.FileOutputStream(targetCutFile), cutType[1],  cutType[2], "", true);
                //new ImageUtil().cutImage(targetCutFile,targetFile, (cutType[1]-cutType[3])/2,  (cutType[2]-cutType[4])/2, cutType[3],  cutType[4]);

                BufferedImage zoom = PictureUtils.thumbnailImage(localFile, cutType[1],  cutType[2]);

                //BufferedImage zoom = PictureUtils.zoom(bi, cutType[1],  cutType[2]);
                BufferedImage subimage = zoom.getSubimage((cutType[1]-cutType[3])/2,  (cutType[2]-cutType[4])/2, cutType[3],  cutType[4]);
                PictureUtils.writeImage(subimage, targetFile, suffix);

            }catch (Exception e){
               e.printStackTrace();
            }
        }

    }

    public static List<int[]> getImageCutType(){
        if(imageCutType.size() != 5 ){
            imageCutType.add(new int[]{1,640,1138,640,960});
            imageCutType.add(new int[]{3,1440,2560,1440,2392});
            imageCutType.add(new int[]{4,1242,2208,1242,2208});
            imageCutType.add(new int[]{5,1370,2436,1125,2436});
            imageCutType.add(new int[]{6,1620,2880,1440,2880});
        }
        return imageCutType;
    }
}
